{if !_G('in_ajax')}
{template modoer_header}
<div id="body">
<div class="myhead"></div>
<div class="mymiddle">
	<div class="myleft">
		<!--{template 'menu','member','member'}-->
	</div>
	<div class="myright">
		<div class="myright_top"></div>
		<div class="myright_middle">
			<h3>空间设置</h3>
			<div class="mainrail">
				<ul class="optabs">
					<li><a href="{url space/member/ac/$ac}">设置</a></li>
					<li class="active"><a href="{url space/member/ac/$ac/op/upload_background}">背景图片</a></li>
				</ul>
				<div class="clear"></div>
				<div style="border-top:1px solid #eee; height:1px; width:98%; margin:0 auto 10px;"></div>
{/if}
				<form method="post" action="{url space/member/ac/$ac/op/$op/rand/$_G[random]}">
				{if $error}<div class="formerror">$error</div>{/if}
				<table width="100%" cellspacing="0" cellpadding="0" class="maintable">
					<tr>
						<td align="right" width="10"></td>
						<td width="*">
							<div class="J_upload_image"></div>
						</td>
					</tr>
				</table>
				</form>
<!--{if !_G('in_ajax')}-->
			</div>
		</div>
	</div>
	<div class="clear"></div>
</div>
<div class="mybottom"></div>
</div>
<!--{/if}-->
<form class="J_form_upload none" method="post" action="{url space/member/ac/$ac/op/$op/upload/Y}" enctype="multipart/form-data">
<input type="file" name="picture" accept="image/*" />
<input type="hidden" name="set_domain" value="N" />
<input type="hidden" name="dosubmit" value="Y" />
</form>
<script type="text/javascript">
$(document).ready(function() {
	var bg_url = '{img $space->config->bg_url}';
	if(bg_url != '') {
		create_background_image(bg_url);
	} else {
		create_upload_button();
	}
	
	$('form.J_form_upload > [name="picture"]').change(function(event) {
		if($(this).val()=='') return;
		var form = $('form.J_form_upload');
		//无刷新上传图片，并获得图片的URL
		msgOpen('正在上传图片，请稍候...');
		var iframe = new $.mframe_form(form, {}, {
			onSucceed:function(data) {
				msgClose();
				if(data.is_message()) {
					myAlert(data);
				} else {
					if(!data.is_json()) {
						alert('系统访问失败，请稍后再尝试。');
						return;
					}
					var json = parse_json(data);
					console.debug(json);
					if(json.code != 200) {
						alert(json.message);
					} else {
						create_background_image(json.bg_url);
					}
				}
			}
		});
		iframe.submit();
	});

	function create_background_image(picurl) {
		var bg_img = $('<img />').attr({src:picurl,title:'点击重新上传背景图片'})
				.css({maxWidth:'400px',cursor:'pointer'})
				.click(function() {
					$('[name="picture"]').click();
		});
		var reset_btn = $('<button type="button">恢复默认背景</button>').click(function() {
			var post = "{url space/member/ac/$ac/op/upload_background}";
			$.post(post.url(), {reset:'Y',in_ajax:1}, function(data, textStatus, xhr) {
				if(data.is_message()) {
					myAlert(data);
				} else {
					if(!data.is_json()) {
						alert('系统访问失败，请稍后再尝试。');
						return;
					}
					var json = parse_json(data);
					console.debug(json);
					if(json.code != 200) {
						alert(json.message);
					} else {
						create_upload_button();
					}
				}
			});
		});
		$('div.J_upload_image').empty().append(bg_img).append('<br /><br />').append(reset_btn);
	}

	function create_upload_button() {
		var button = $('<button type="button">上传背景图片</button>').click(function() {
			$('[name="picture"]').click();
		});
		$('div.J_upload_image').empty().append(button);
	}
});
</script>
<!--{if !_G('in_ajax')}-->
<!--{eval footer();}-->
<!--{/if}-->